
/*
The MIT License (MIT)

Copyright (c) 2014 firendlys.All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

License:	http://opensource.org/licenses/MIT
Project:	http://firendlys-projs.googlecode.com/svn/
**/

#include <myFn13/myFnCommonHeadFile.h>

#ifdef _WIN_
#include <WinSock2.h>
#include <MSWSock.h>
#include <MSTcpIP.h>
#include <Windows.h>
#pragma comment(lib, "ws2_32.lib")

#else

#endif

#include <myFn13/myNetworkInterface.h>

namespace myFn
{
	namespace v1
	{
		using namespace inf;
		namespace network
		{
			//------------------------------------------------------
			// class IPAddr
			IPAddr::IPAddr()
			{
				zero();
			}
			void IPAddr::zero()
			{
				memset(this, 0, sizeof(*this));
			}

			//-------------------------------------------------------
			
			myTcpServerInterface::myTcpServerInterface()
			{
				m_iListenPort = 0;
				m_socket = 0;
			}
			myTcpServerInterface::~myTcpServerInterface()
			{

			}
			bool myTcpServerInterface::SetListenPort(int port)
			{
				if (port < 0 || port > 0xFFFF)
				{
					return false;
				}
				m_iListenPort = (unsigned short)port;
				return true;
			}






			//-------------------------------------------------------


			namespace
			{
				static int s_local_ips_count = 0;
				static IPAddr s_local_ips[32];

			};

			IPAddr GetLocalIP()
			{
				if (s_local_ips_count <= 0)
				{
					GetLocalIPs(nullptr, true);
				}
				if (s_local_ips_count >= 0)
				{
					return s_local_ips[0];
				}
				return IPAddr();
				
			}

			int GetLocalIPs(IPAddr ips[32], bool isRefresh)
			{
				if(s_local_ips_count == 0 || isRefresh)
				{

#ifdef _WIN_
					char name[256];
					int i = 0;
					struct hostent *hostinfo = nullptr;

					if(gethostname(name, sizeof(name) - 1) == 0)
					{
						hostinfo = gethostbyname(name);

						if(hostinfo)
						{
							while(hostinfo->h_addr_list[i])
							{
								s_local_ips[s_local_ips_count].ipv4 = *(DWORD *)(void *)hostinfo->h_addr_list[i];
								++s_local_ips_count;

								if(s_local_ips_count >= 32)
								{
									break;
								}

								++i;
							}
						}
					}

#elif defined(__linux__)
					int fd = NPCRT_API_SOCKET(AF_INET, SOCK_DGRAM, 0);

					if(fd <= 0)
					{
						return 0;
					}

					struct ifreq buf[16];

					struct ifconf ifc;

					ifc.ifc_len = sizeof(buf);

					ifc.ifc_buf = (caddr_t)buf;

					if(0 == ioctl(fd, SIOCGIFCONF, (char *)(void *)&ifc))
					{
						int intrface = ifc.ifc_len / sizeof(struct ifreq);

						for(int i = 0; i < intrface; i++)
						{
							if(0 != ioctl(fd, SIOCGIFADDR, (char *)&buf[i]))
							{
								continue;
							}

							DWORD ip = (DWORD)((struct sockaddr_in *)(&buf[i].ifr_addr))->sin_addr.s_addr;

							if(ip == ADDR_ANY || htonl(INADDR_LOOPBACK) == ip || INADDR_NONE == ip)
							{
								continue;
							}

							s_local_ips[s_local_ips_count].ipv4 = ip;
							++s_local_ips_count;

							if(s_local_ips_count > 32)
							{
								break;
							}
						}
					}

					assert(0 == FDClose(fd));
#endif

				}

				assert(s_local_ips_count < 32);
				if (ips)
				{
					memcpy(ips, s_local_ips, sizeof(s_local_ips));
				}
				return s_local_ips_count;
			};


			bool is_invalid_socket(SOCKET &s)
			{
				if (s==0||s==INVALID_SOCKET)
				{
					return true;
				}
				return false;
			}

			void close_socket(SOCKET &s)
			{
				if (s==0 || s==INVALID_SOCKET)
				{
					return;
				}
				::closesocket(s);
				s = INVALID_SOCKET;
				return;
			}

		};


	};
};


